/**
 * Created by duwei on 2019/4/4.
 */
var ZiPaiRoomNX = ZiPaiJavaRoom.extend({
    _ccsFileName: zipai.res.ZiPaiRoom_qylds_json,
    isBaiPai:false,
    fanxing:false,
    isBlockFapai:false,
    isFapai:false,
    initLog: function () {
        var self = this;
        var game_title = self.$('RoomInfo.game_title');
        var resLog = zipai.res.logo_NXPHZ;
        game_title.setTexture(resLog);
    },
    ctor: function (data, roomDataType) {
        var self = this;
        self.initInfo();
        self._super(data, roomDataType);
        return true;
    },
    initInfo : function () {
        console.log("ZiPaiRoomNX initInfo");
        zipai.curBaipaiLength = zipai.mapid2baiPaiLength[gameData.mapId] ? zipai.mapid2baiPaiLength[gameData.mapId] : zipai.curBaipaiLength;
        zipai.curMaxPlayerNum = zipai.mapid2maxPlayerNum[gameData.mapId] ? zipai.mapid2maxPlayerNum[gameData.mapId] : zipai.curMaxPlayerNum;
        //设置语言为普通话
        var local_dialect = settingData.local_dialect;
        local_dialect[gameData.mapId] = false;
        settingData.local_dialect = local_dialect;
    },
    /**
     * 胡状态时，桌面显示
     * @param inData 4008数据
     * @return false:不需要显示对应胡牌id
     */
    changeZhuomianWithHu: function(inData){
        var self = this;
        var GRData = deepCopy(inData);
        var players = GRData.players;
        var curPlayer = null;
        for (var i = 0; i < players.length; i++) {
            if (players[i].uid == gameData.uid && players[i].hu) {
                curPlayer = players[i];
            }
        }
        if (curPlayer == null) return false;
        //对牌
        var huShoupai = curPlayer.shoupai || [];
        var curShoupai =  self.zipaiManager.SeverCardList;
        if (huShoupai.length == curShoupai.length) {
            return false;
        }
        /*
        else {
            //判断是别人抓的
            if (self.isHuOtherPerson == true) {
                return false;
            }
            else {
                return true;
            }
        }
         */

        return true;
    },
    /**
     * 判断倾、啸胡
     * @constructor
     */
    IsQing1XiaoHu: function(){
        var GSData = deepCopy(this.zipaiManager.gameOverData);
        var players = GSData.players;
        var hupaiId = GSData.hu_pai_id;
        var huPlayer = null;
        for (var i = 0; i < players.length; i++) {
            if (players[i].hu) {
                huPlayer = players[i];
            }
        }

        var huUid = huPlayer.uid;
        var openCards = this.zipaiManager.g_uid_to_opencards[huUid] || [];
        if (openCards.length == 0 || openCards == undefined || openCards == null) return false;
        var lastCards = {};
        for (var i = 0; i < openCards.length; i++) {
            lastCards = openCards[i].pai_arr;
            if (lastCards.length == 3 || lastCards.length == 4) {
                if (lastCards[0] == hupaiId && lastCards[1] == hupaiId && lastCards[2] == hupaiId) {
                    if (lastCards.length == 4) {
                        return true;
                    }
                    else {
                        //手牌中对应的换掉了赖子，不存在hupaiId
                        var huShoupai = huPlayer.shoupai;
                        if (huShoupai.indexOf(hupaiId) > -1){
                            return false;
                        }
                        return true;
                    }
                } else {
                    continue;
                }
            } else {
                return false;
            }
        }
    },
    /**
     * 小结算
     * 4008
     */
    /**
     *
     * @param isPlayMusic 播放音效
     * @param isJumpXingpai 不跳过醒牌
     * @param isJumpDelayTime 跳过延时到小结
     */
    showGameResult: function (isPlayMusic, isJumpXingpai, isJumpDelayTime) {
        var that = this;
        /*
        //准备状态设置成0
        for (var key in that.zipaiManager.g_uid_to_ready){
            that.zipaiManager.g_uid_to_ready.key = 0;
        }
         */

        cc.sys.localStorage.setItem('Storage_handsCards', []);

        //#region 改变手牌
        var isShowHupaiId = that.changeZhuomianWithHu(this.zipaiManager.gameOverData);
        //#endregion
        var delayt = 0.5;
        if(this.zipaiManager.gameOverData &&this.zipaiManager.gameOverData.xingPaiId &&
            (cc.isUndefined(isJumpXingpai) || isJumpXingpai == false)){
            var game_zhang = that.$('RoomInfo.game_zhang');
            /*
            mEffect.fanxing(this.zipaiManager.gameOverData.xingPaiId, game_zhang,this.zipaiManager.gameOverData.hu_pai_id,
                false, isShowHupaiId);
             */
            var isSelfHu = false;
            var tmpPlayersData = this.zipaiManager.gameOverData.players;
            for (var i = 0; i < tmpPlayersData.length; i++){
                if (tmpPlayersData[i].hu == true && tmpPlayersData[i].uid == gameData.uid) {
                    isSelfHu = true;
                    break;
                }
            }
            //倾、啸胡，删除亮牌
            var isQing1Xiaohu = that.IsQing1XiaoHu();
            if (isQing1Xiaohu == true) {
                this.showCard.setVisible(false);
            }

            var isWangfanhu = that.checkWangfan(that.zipaiManager.gameOverData.fanInfo);
            var call = function(){
                mEffect.fanxing(this.zipaiManager.gameOverData.xingPaiId, game_zhang,this.zipaiManager.gameOverData.hu_pai_id,
                    false, isSelfHu, isShowHupaiId, isWangfanhu);
            }

            delayt = isWangfanhu ? 1.8 : 0.5;
            var delayt2 = isWangfanhu ? 0.8 : 0.5;
            that.scheduleOnce(function () {
                mEffect.fanhu(this.zipaiManager.gameOverData.xingPaiId, game_zhang,this.zipaiManager.gameOverData.hu_pai_id,
                    false, isSelfHu, isShowHupaiId, isWangfanhu);
            }, delayt2);
            that.scheduleOnce(function () {
                mEffect.handleGuangquanAni(game_zhang,call);
            }, delayt);
        }

        var huType = that.zipaiManager.gameOverData['hu_type'];
        if (huType == 3) {
            setTimeout(function () {
                var touchLayer = that.getTouchLayer();
                cc.eventManager.resumeTarget(touchLayer, true);
                that.showRoomClean();
                that.onReady();
            }, 2000);
            return;
        }

        var delayTime = isJumpDelayTime ? 0 : 3000+delayt*1000;

        setTimeout(function () {
            that.setTurnInOver();
            that.showOtherPai();
            var hu_type = that.zipaiManager.gameOverData['hu_type'];
            var xiaojieData = deepCopy(that.zipaiManager.gameOverData);
            if (hu_type != 0){
                showWindow('ZiPai_JieSuanJavaWindow_qylds', xiaojieData, that);
            }
            if (isPlayMusic) {
                if (hu_type == 0) {
                    playEffect('huangz');//胡不得
                } else {
                    //that.playCPTPHEffect('hu', gameData.sex);
                }
                playEffect('getw');
            }
            that.panelResult.setVisible(!that.zipaiManager.isReplay);
            if (hu_type == 0) {
                that.handleHuangzhuang();
            }
        },delayTime);
    },
    handleWanfaDesp: function (wanfaDesp){
        var removedStrArr =['王钓麻将', '落地扫', '永州扯胡子', '2人PK', '3人激情', '2人玩', '3人玩', '4人玩',
            '局数: 6局', '局数: 8局', '局数: 10局', '群主支付', '房主支付', 'AA支付'];
        var retVal = wanfaDesp;
        var retValArr = retVal.split(',');
        var locationArr = [];
        for (var i = 0; i < retValArr.length; i++){
            retValArr[i] = retValArr[i].trim();
            if (removedStrArr.indexOf(retValArr[i]) > -1) locationArr.push(i);
        }

        for (var i = locationArr.length - 1; i > -1; i--){
            retValArr.splice(locationArr[i], 1);
        }

        retVal = retValArr.join(',');

        return retVal;
    },
    initializeUI: function () {
        var self = this;
        this.startTime();
        this.startSignal();
        this.txtRound = self.$('guding.txtRound');
        self.$('guding.lb_roomid').setString(this.zipaiManager.RoomID);
        self.$('RoomInfo.lb_wanfa').setString(self.handleWanfaDesp(this.zipaiManager.WanfaDesp));
        if (gameData.playerNum){
            if (gameData.playerNum == 2){
                self.$('guding.txt_XChangci').setVisible(true);
                self.$('guding.txt_XChangci').setTexture(zipai.res.title_LDS_2);
            }
            else if (gameData.playerNum == 3){
                self.$('guding.txt_XChangci').setVisible(true);
                self.$('guding.txt_XChangci').setTexture(zipai.res.title_LDS_3);
            }
            else if (gameData.playerNum == 4){
                self.$('guding.txt_XChangci').setVisible(true);
                self.$('guding.txt_XChangci').setTexture(zipai.res.title_LDS_4);
            }
            else {
                self.$('guding.txt_XChangci').setVisible(false);
            }
        }
        else{
            self.$('guding.txt_XChangci').setVisible(false);
        }


        this.game_zhang = self.$('RoomInfo.game_zhang');
        this.txtCardCount = self.$('RoomInfo.game_zhang.txtCardCount');

        this.jushou = self.$('jushou');
        this.bujushou = self.$('bujushou');
        this.jushou.setVisible(false);
        this.bujushou.setVisible(false);

        this.cd = self.$('RoomInfo.cd');
        this.cdtext = self.$('RoomInfo.cd.cdtext');
        this.cd.setVisible(false);
        //进度条指针
        var cdbar = new cc.ProgressTimer(cc.Sprite.create(zipai.dynamicRes + 'greenquan.png'));
        cdbar.setPosition(cc.p(this.cd.getContentSize().width / 2, this.cd.getContentSize().height / 2 + 2));
        cdbar.setType(cc.ProgressTimer.TYPE_RADIAL);
        cdbar.setMidpoint(cc.p(0.5, 0.5));
        cdbar.setReverseDirection(true);
        cdbar.runAction(
            cc.progressTo(0.1, 100)
        );
        this.cd.addChild(cdbar);
        this.cdbar = cdbar;

        this.tingNode = self.$('ting_node');

        this.tipLine = self.$('RoomInfo.tipLine');
        this.tipLine.setVisible(false);
        this.CHUPAI_HEIGHT = this.tipLine.getPositionY();

        this.nAction = self.$('nAction');
        this.listActBtn = [];
        var btnName = {
            1 : "hu",
            2 : "chi",
            3 : "peng",
            4 : "guo",
            5 : "guo",
            6 : "wangdiao",
            7 : "wangchuang",
            8 : "wangzha"
        };
        for (var i = 1; i <= 8; i++) {
            (function (i) {
                var bt = self.$('nAction.bt' + i);
                var aniPos = self.$('aniPos', bt);
                if (btnName[i]) {
                    aniPos.removeAllChildren();
                    //playAnimScene(aniPos, zipai.res[btnName[i]], 0, 0, true, function () {});
                    self.showBtnActAnimation(aniPos, btnName[i], true);
                }
                bt.setVisible(false);
                TouchUtil.setOnclickListener(bt, function () {
                    self.onActionBtn(i, bt);
                }, {});
                self.listActBtn.push(bt);
            })(i);
        }

        self.$('FunctionBtn.bottomCenter.btn_zhunbei').setVisible(false);
        self.$('FunctionBtn.btn_refresh').setVisible(false);

        self.imgAct = self.$('imgAct');
        self.nLaizi = self.$('nLaizi');
        self.nChiList = self.$('nChiList');
        self.nOpenOutLayer = self.$('nOpenOutLayer');
        self.huNode = self.$('HuNode', this.nOpenOutLayer);


        this.panelResult = self.$('PanelResult');
        this.panelResult.setLocalZOrder(10);
        this.panelResult.setVisible(false);

        this.btn_continue = self.$('PanelResult.continue');

        TouchUtil.setOnclickListener(self.$('PanelResult.btn_zhuomian'), function () {
            var touchLayer = self.getTouchLayer();
            cc.eventManager.resumeTarget(touchLayer, true);
            self.closeXiaojieZhuomian();
            self.showGameResult(false, true, true);
        });
        TouchUtil.setOnclickListener(this.btn_continue, function () {
            var touchLayer = self.getTouchLayer();
            cc.eventManager.resumeTarget(touchLayer, true);
            self.closeXiaojieZhuomian();
            self.showRoomClean();
            self.onReady();
        });

        self.initPlayerInfoNode();
        self.enableChuPai();
        self.clearRoom();
        self.changeBgm();
        this.countDown = this.countDown();
        self.initLog();
    },
    /**
     * 初始化界面
     */
    initPlayerInfoNode: function () {
        var self = this;
        self.playersNum = this.zipaiManager.PlayersNum;
        self.PlayerInfoNode = self.$('PlayerInfo.PlayerInfoNode.table' + zipai.curMaxPlayerNum);
        self.PlayerInfoNode.setVisible(true);
        self.PlayerInfoNode.setSwallowTouches(false);
        self.PosNode = self.$('pos' + self.playersNum, self.PlayerInfoNode);
        //PanelResult.bg.containner
        self.XiaojieShowPosNode = self.$('PanelResult.bg.containner');
        //table4.pos位置变更
        if (zipai.curMaxPlayerNum == 4) {
            if (self.playersNum == 3){
                var tmpName = "tmpName";
                //info
                self.$("info3",self.PlayerInfoNode ).setName(tmpName);
                self.$("info1",self.PlayerInfoNode ).setName("info3");
                self.$("info2",self.PlayerInfoNode ).setName("info1");
                self.$(tmpName,self.PlayerInfoNode ).setName("info2");
                //replay_act
                self.$("replay_act3",self.PlayerInfoNode ).setName(tmpName);
                self.$("replay_act1",self.PlayerInfoNode ).setName("replay_act3");
                self.$("replay_act2",self.PlayerInfoNode ).setName("replay_act1");
                self.$(tmpName,self.PlayerInfoNode ).setName("replay_act2");
                //out
                self.$("out3",self.PosNode ).setName(tmpName);
                self.$("out1",self.PosNode ).setName("out3");
                self.$("out2",self.PosNode ).setName("out1");
                self.$(tmpName,self.PosNode ).setName("out2");
                //open
                self.$("open3",self.PosNode ).setName(tmpName);
                self.$("open1",self.PosNode ).setName("open3");
                self.$("open2",self.PosNode ).setName("open1");
                self.$(tmpName,self.PosNode ).setName("open2");
                //nCards0
                self.$("nCards3",self.PosNode ).setName(tmpName);
                self.$("nCards1",self.PosNode ).setName("nCards3");
                self.$("nCards2",self.PosNode ).setName("nCards1");
                self.$(tmpName,self.PosNode ).setName("nCards2");
                //PanelResult.bg.containner
                self.$("pos3",self.XiaojieShowPosNode ).setName(tmpName);
                self.$("pos1",self.XiaojieShowPosNode ).setName("pos3");
                self.$("pos2",self.XiaojieShowPosNode ).setName("pos1");
                self.$(tmpName,self.XiaojieShowPosNode ).setName("pos2");
            }
            else if (self.playersNum == 2){
                var tmpName = "tmpName";
                //info
                self.$("info1",self.PlayerInfoNode ).setName(tmpName);
                self.$("info3",self.PlayerInfoNode ).setName("info1");
                self.$(tmpName,self.PlayerInfoNode ).setName("info3");
                //replay_act
                self.$("replay_act1",self.PlayerInfoNode ).setName(tmpName);
                self.$("replay_act3",self.PlayerInfoNode ).setName("replay_act1");
                self.$(tmpName,self.PlayerInfoNode ).setName("replay_act3");
                //out
                self.$("out1",self.PosNode ).setName(tmpName);
                self.$("out3",self.PosNode ).setName("out1");
                self.$(tmpName,self.PosNode ).setName("out3");
                //open
                self.$("open1",self.PosNode ).setName(tmpName);
                self.$("open3",self.PosNode ).setName("open1");
                self.$(tmpName,self.PosNode ).setName("open3");
                //nCards0
                self.$("nCards1",self.PosNode ).setName(tmpName);
                self.$("nCards3",self.PosNode ).setName("nCards1");
                self.$(tmpName,self.PosNode ).setName("nCards3");
                //PanelResult.bg.containner
                self.$("pos1",self.XiaojieShowPosNode ).setName(tmpName);
                self.$("pos3",self.XiaojieShowPosNode ).setName("pos1");
                self.$(tmpName,self.XiaojieShowPosNode ).setName("pos3");
            }
        }

        for (var i = 0; i < zipai.curMaxPlayerNum; i++) {
            var infoNode = self.$('info' + i, self.PlayerInfoNode);
            infoNode.setVisible(false);
            // infoNode.getChildByName('txtHX').setVisible(false);
            // infoNode.getChildByName('txtHX_value').setVisible(false);
            infoNode.getChildByName('timeBg').setVisible(false);
            infoNode.getChildByName('ChatInfo').setVisible(false);
            infoNode.getChildByName('ok').setVisible(false);
            infoNode.getChildByName('offline').setVisible(false);
            infoNode.getChildByName('zhuang').setVisible(false);
            infoNode.getChildByName('fangzhu').setVisible(false);


            var headf = infoNode.getChildByName('headf');
            var guangquan = playAnimScene(headf, zipai.res.PhzHead_json, headf.getContentSize().width / 2, headf.getContentSize().height / 2, true);
            guangquan.setName('guangquan');
            guangquan.setVisible(false);


            var openNode = new OpenCards();
            openNode.setName('OpenCards');
            openNode.setPos(i);
            self.$('open' + i, this.PosNode).addChild(openNode);


            var outNode = new OutCards();
            outNode.setName('OutCards');
            outNode.setPos(i);
            self.$('out' + i, this.PosNode).addChild(outNode);
        }

        this.actCard = new Card();
        this.addChild(this.actCard);
        this.showCard = new Card();
        this.PlayerInfoNode.getChildByName('showCard').addChild(this.showCard);
    },
    /**
     * 显示吃、碰、过、胡 按钮
     * @param data
     * @param op_map
     */
    showActions: function (data, op_map) {
        var self = this;
        if (!self.zipaiManager.isReplay) {
            self.nAction.setVisible(true);
            var show_options = [];
            var options = op_map;
            if (options[self.opType.OP_PASS].length > 0) {
                var guoBtn = this.listActBtn[3];
                guoBtn.paiArr = options[self.opType.OP_PASS];
                show_options.push(guoBtn);
            }
            if (options[self.opType.OP_CHI].length > 0) {
                var chiBtn = this.listActBtn[1];
                chiBtn.paiArr = options[self.opType.OP_CHI];
                show_options.push(chiBtn);
            }
            if (options[self.opType.OP_PENG].length > 0) {
                var pengBtn = this.listActBtn[2];
                pengBtn.paiArr = options[self.opType.OP_PENG];
                show_options.push(pengBtn);
            }
            if (options[self.opType.OP_HU].length > 0) {
                var huBtn = this.listActBtn[0];
                huBtn.paiArr = options[self.opType.OP_HU];
                show_options.push(huBtn);
            }
            if (options[self.opType.OP_WANGDIAO].length > 0){
                var wangdiaoBtn = this.listActBtn[5];
                wangdiaoBtn.paiArr = options[self.opType.OP_WANGDIAO];
                show_options.push(wangdiaoBtn);
            }
            if (options[self.opType.OP_WANGCHUANG].length > 0){
                var wangchuangBtn = this.listActBtn[6];
                wangchuangBtn.paiArr = options[self.opType.OP_WANGCHUANG];
                show_options.push(wangchuangBtn);
            }
            if (options[self.opType.OP_WANGZHA].length > 0){
                var wangzhaBtn = this.listActBtn[7];
                wangzhaBtn.paiArr = options[self.opType.OP_WANGZHA];
                show_options.push(wangzhaBtn);
            }
            for (var i = 0; i < show_options.length; i++) {
                var bt = show_options[i];
                bt.setPosition(i * -250, 0);
                bt.setVisible(true);
            }

            var tmpPSize = self.nAction.getParent().getContentSize();
            var tmpXOffset = (show_options.length - 1) * 250 / 2;
            self.nAction.setPosition(tmpPSize.width / 2 + tmpXOffset, tmpPSize.height / 2);
        } else {
            var pos = self.zipaiManager.g_uid_to_pos[data['uid']];
            var replay_act = self.$('replay_act' + pos, self.PlayerInfoNode);
            if (!!replay_act) {
                replay_act.setVisible(true);
            }
        }
    },
    /**
     * 切换牌桌背景图
     */
    changeBg: function () {
        if(cc.isUndefined(settingData.local_table[gameData.mapId])){
            var local_table = settingData.local_table;
            local_table[gameData.mapId] = 0;
            settingData.local_table = local_table;
        }
        var sceneid = settingData.local_table[gameData.mapId];
        var bg = this.$('RoomInfo.bg');
        bg.setTexture(zipai.res['roomBg_' + sceneid]);
        //this.setCardBackImg();
    },
    playEffectByMap: function (card, sex) {
        playEffect('qiyang_v' + card, sex);
    },
    playCPTPHEffect: function (cp, sex) {
        playEffect('qiyang_v' + cp, sex);
    },
    /**
     * 显示吃碰杠胡按钮
     * 4005
     * @param data
     */
    onChiPengGangHu: function (data) {
        var self = this;
        self.isTurnOut = false;
        self.isTurnIn = true;
        self.secondsLeft = 15;
        var option_type = {
            0: self.opType.OP_CHI,
            1: self.opType.OP_PENG,
            2: self.opType.OP_HU,
            3: self.opType.OP_WANGDIAO,
            4: self.opType.OP_WANGCHUANG,
            5: self.opType.OP_WANGZHA,
            6: self.opType.OP_PASS
        };
        var op_map = {};
        for (var key in self.opType) {
            if (self.opType.hasOwnProperty(key)) {
                op_map[self.opType[key]] = [];
            }
        }
        var tishi = data['tishi'];
        var wangFanNum = 0;
        if (tishi[0]['wangFan'] && tishi[0]['wangFan'].length > 0) {
            wangFanNum = tishi[0]['wangFan'].length;
            self.showNo = data['tishi'][0].pai_id;
        }
        if (tishi[0]['op'][0] == false && tishi[0]['op'][1] == false && tishi[0]['op'][2] == true) {
            //检测手牌里是否有王霸
            var paiArr = this.zipaiManager.getSeverCardList();
            var has21 = false;
            for (var i = 0; i < paiArr.length; i++){
                if (paiArr[i] == 21){
                    has21 = true;
                    break;
                }
            }
            if (wangFanNum > 0){
                op_map[self.opType.OP_PASS].push(0);
            }
            else {
                if (has21) {
                    op_map[self.opType.OP_PASS].push(0);
                }
            }
        }
        else {
            op_map[self.opType.OP_PASS].push(0);
        }
        self.showNo = tishi[0]['pai_id'];
        for (var index = 0; index < tishi.length; index++) {
            var pai_id = tishi[index]['pai_id'];
            var op = tishi[index]['op'] || [false, false, false];
            for (var op_idx = 0; op_idx < op.length; op_idx++) {
                if (op[op_idx]) {
                    //带王的番型
                    if (op_idx == 2 && op[op_idx] == true && wangFanNum > 0) {
                        if (tishi[index]["wangFan"].indexOf(21) > -1) op_map[self.opType.OP_WANGDIAO].push(pai_id);
                        if (tishi[index]["wangFan"].indexOf(22) > -1) op_map[self.opType.OP_WANGCHUANG].push(pai_id);
                        if (tishi[index]["wangFan"].indexOf(23) > -1) op_map[self.opType.OP_WANGZHA].push(pai_id);
                    }
                    else {
                        op_map[option_type[op_idx]].push(pai_id);
                    }
                }
            }
        }
        self.showActions(data, op_map);
    },
    getTouchLayer : function () {
        return this.getChildByName("touchLayer");
    },
    /**
     * 处理小结返回桌面显示结算各玩家的手牌
     */
    showXiaojieZhuomain : function () {
        var self = this;
        /*
        var game_zhang = self.$('RoomInfo.game_zhang');
        if (game_zhang.getParent().getChildByName('hucard')) {
            game_zhang.getParent().getChildByName('hucard').removeFromParent();
        }
        */
        var players = self.zipaiManager.gameOverData.players;
        var playCount = players.length;
        var hupaiId = self.zipaiManager.gameOverData.hu_pai_id;
        /*
        //移出王闯王钓王炸的牌
        var cardsNode = self.$('PlayerInfo.node_wang');
        if(cardsNode){
            cardsNode.removeAllChildren();
        }
         */
        //如果胡他人牌，删除自己桌面的胡牌
        if (self.isSelfHuOtherPerson == true) {
            var game_zhang = self.$('RoomInfo.game_zhang');
            if (cc.sys.isObjectValid(game_zhang.getParent().getChildByName('hucard'))) {
                game_zhang.getParent().getChildByName('hucard').removeFromParent();
            }
        }

        //王钓王闯王炸 他人显示亮牌
        if (self.isWangfanHu == true){
            for (var i = 0; i < players.length; i++) {
                if (players[i].hu == true) {
                    if (players[i].uid != gameData.uid) {
                        var tmpParent = self.showCard.getParent();
                        self.showCard.removeFromParent();
                        var huLocation = self.zipaiManager.g_uid_to_pos[players[i].uid];

                        var huPos = cc.p(DesignSize.width / 2, DesignSize.height / 2);
                        if (players.length == 4){
                            if (huLocation == 1) huPos =  cc.p(DesignSize.width - 450, 225);
                            if (huLocation == 2) huPos =  cc.p(DesignSize.width - 450, DesignSize.height - 225);
                            if (huLocation == 3) huPos =  cc.p(450, DesignSize.height - 225);
                        }
                        else if(players.length == 3){
                            if (huLocation == 1) huPos =  cc.p(DesignSize.width - 450, DesignSize.height - 225);
                            if (huLocation == 2) huPos =  cc.p(450, DesignSize.height - 225);
                        }
                        else if(players.length == 2){
                            if (huLocation == 1) huPos =  cc.p(450, DesignSize.height - 225);
                        }

                        self.showCard = new Card();
                        self.showCard.setData(hupaiId, null, 1);
                        self.showCard.setPosition(huPos);
                        tmpParent.addChild(self.showCard);
                    }
                }
            }
        }
        //显示其他人的手牌
        var containner = self.$('PanelResult.bg.containner');
        //var row = self.zipaiManager.g_uid_to_pos[uid];
        var row = 0;
        var pos;
        var card;
        var playerData;
        var showPai = [];
        for (var i = 0; i < playCount; i ++){
            playerData = players[i];
            if (playerData.uid != gameData.uid) {
                row = self.zipaiManager.g_uid_to_pos[players[i].uid];
                pos = self.$("pos" + row + ".cardPos", containner);
                if (playerData.hu) {
                    var tmpShoupai = deepCopy(playerData.shoupai);
                    var tmpLocation = tmpShoupai.indexOf(self.zipaiManager.gameOverData.hu_pai_id);
                    if (self.zipaiManager.gameOverData.hu_pai_id != 21) {
                        var tmpLocation = tmpShoupai.indexOf(self.zipaiManager.gameOverData.hu_pai_id);
                        if (tmpLocation > -1) {
                            tmpShoupai.splice(tmpLocation, 1);
                        }
                    }
                    var wangCount = 0;
                    if (self.isWangfanHu == true){
                        if (self.wangfanType == self.opType.OP_WANGDIAO) wangCount = 1;
                        else if (self.wangfanType == self.opType.OP_WANGCHUANG) wangCount = 2;
                        else if (self.wangfanType == self.opType.OP_WANGZHA) wangCount = 3;
                    }
                    for (var m = 0; m < wangCount; m++) {
                        var tmpLocation = tmpShoupai.indexOf(21);
                        if (tmpLocation > -1) {
                            tmpShoupai.splice(tmpLocation, 1);
                        }
                    }
                    showPai = mCard.getCardList(tmpShoupai);
                }
                else {
                    showPai = mCard.getCardList(playerData.shoupai);
                }


                for (var m = 0; m < showPai.length; m++) {
                    for (var n = 0; n < showPai[m].length; n++) {
                        card = new Card();
                        pos.addChild(card);
                        card.setData(showPai[m][n], null, 2);
                        card.setAnchorPoint(cc.p(0, 0));
                        card.setPosition(cc.p((m - 1) * 42, (n - 1) * 42));
                    }
                }
            }
            else {
                if (playerData.hu) {
                    var menzis = deepCopy(playerData.duiArr);
                    var shoupai = deepCopy(playerData.shoupai);

                    var laizi = deepCopy(self.zipaiManager.gameOverData.laizi);
                    var shoupaiDuiArr = self.getShoupaiPaidui(shoupai, menzis, laizi);
                    var hupaiId = self.zipaiManager.gameOverData.hu_pai_id;
                    var isFoundHuId = false;
                    for (var m = 0; m  < shoupaiDuiArr.length; m++) {
                        if (isFoundHuId == true) break;
                        for (var n = 0; n < shoupaiDuiArr[m].length; n++) {
                            if (shoupaiDuiArr[m][n] == hupaiId && hupaiId != 21) {
                                shoupaiDuiArr[m].splice(n, 1);
                                isFoundHuId = true;
                                break;
                            }
                        }
                    }

                    var wangCount = 0;
                    if (self.isWangfanHu == true){
                        if (self.wangfanType == self.opType.OP_WANGDIAO) wangCount = 1;
                        else if (self.wangfanType == self.opType.OP_WANGCHUANG) wangCount = 2;
                        else if (self.wangfanType == self.opType.OP_WANGZHA) wangCount = 3;
                    }

                    for (var m = 0; m  < shoupaiDuiArr.length; m++) {
                        for (var n = shoupaiDuiArr[m].length -1; n > -1; n--) {
                            if (shoupaiDuiArr[m][n] == 21 && wangCount > 0) {
                                shoupaiDuiArr[m].splice(n, 1);
                                wangCount --;
                            }
                        }
                    }

                    this.zipaiManager.CardList = shoupaiDuiArr;
                    this.setUpCards();
                }
            }
        }
        var dipai = self.$('dipai', containner);
        dipai.setVisible(true);
        //显示底牌
        var pais = self.zipaiManager.gameOverData.dipai_arr;
        var paiLen = pais.length;
        var txt_dipai = self.$("txt_dipai", dipai);
        pos = self.$("pos", dipai);
        var lineNum = 1; // 1 2
        var maxNum = 15; // 15 20 27
        var cardSizeType = 2;
        var num1max = 15;// 1  42  15
        var num2max = 40;// 2  34  20
        // 2 34 27
        var offsetX = 0;
        var offsetY = 0;
        //设置摆放类型
        if (paiLen <= num1max){
            lineNum = 1;
            maxNum = paiLen;
            cardSizeType = 2;
        }
        else if (paiLen <= num2max){
            lineNum = 2;
            maxNum = 20;
            cardSizeType = 3;
        }
        else if (paiLen > num2max){
            lineNum = 2;
            maxNum = 27;
            cardSizeType = 3;
        }
        else {
            return;
        }

        //设置偏移值
        if (lineNum == 1){
            //90是"底牌"的位置
            offsetX = (90 + maxNum * 42) / 2;
            offsetY = 0;
        }
        else{
            offsetX = (90 + maxNum * 34) / 2;
            offsetY = 34 / 2;
        }

        txt_dipai.setPositionX(1280 / 2 - offsetX);
        pos.setPositionX(1280 / 2 - offsetX + 90);

        //创建卡
        for (var i = 0; i < paiLen; i++) {
            var card = new Card();
            card.setData(pais[i], null, cardSizeType == 2 ? 2 : 3);
            pos.addChild(card);
            card.setAnchorPoint(0, 0.5);
            if (i < maxNum) {
                if (cardSizeType == 2){
                    card.setPosition(cc.p(i * 42, 0));
                }
                else {
                    card.setPosition(cc.p(i * 34, offsetY));
                }
            }
            else {
                card.setPosition(cc.p((i - maxNum) * 34, -offsetY));
            }
        }

        //亮牌区处理
        //self.zipaiManager.gameOverData
        var tmpPlayers = self.zipaiManager.gameOverData.players;
        var tmpUid = -1;
        var opencardsData = [];
        var playerPos = -1;
        var opencardNode = null;
        for (var i = 0; i < tmpPlayers.length; i++) {
            tmpUid = tmpPlayers[i].uid;
            if (tmpUid != gameData.uid) {
                playerPos = self.zipaiManager.g_uid_to_pos[tmpUid];
                opencardsData = self.zipaiManager.g_uid_to_opencards[tmpUid] || [];
                opencardNode = self.getOpenNode(playerPos);
                opencardNode.setPos(playerPos);
                for (var j = 0; j < opencardsData.length; j++) {
                    var menzi = opencardsData[j];
                    var cards = menzi['pai_arr'];
                    //if (menzi['type'] == 5 || menzi['type'] == 6) menzi['type'] = 4;
                    //if (menzi['type'] == 7) menzi['type'] = 8;
                    var typ = mCard.getJavaComboType(menzi['type']);
                    opencardNode.addCards(cards, typ, false, menzi['chiPaiId'], null, true);
                }
            }
        }
    },
    /**
     * 获取手牌牌对
     * @param shoupai
     * @param allDuiArr
     * @param laizi
     */
    getShoupaiPaidui : function(shoupai, allDuiArr, laizi) {
        //替换手牌赖子
        var retDuiArr = [];
        var laiziCount = laizi.length;
        var shoupaiLen = shoupai.length;
        var tmpLaizi = deepCopy(laizi);
        if (laiziCount > 0){
            for (var i = 0; i < shoupaiLen; i++){
                if (shoupai[i] == 21){
                    var tmplz = tmpLaizi.pop();
                    if (tmplz != undefined) {
                        shoupai[i] = tmplz;
                    }
                }
            }
        }

        //查找手牌对应的对牌
        var duiLen = 0;
        var indexArr = [];
        var dui = {};
        var tmpShoupai = [];
        var paiIndex = -1;
        for (var i = 0; i < allDuiArr.length; i ++){
            indexArr = [];
            dui = allDuiArr[i];
            duiLen = dui.pai_arr.length;

            tmpShoupai = deepCopy(shoupai);
            for (var j = 0; j < duiLen; j++){
                paiIndex = tmpShoupai.indexOf(dui.pai_arr[j]);
                if(paiIndex != -1){
                   tmpShoupai.splice(paiIndex, 1);
                }
            }
            if (shoupai.length - tmpShoupai.length == duiLen){
                shoupai = tmpShoupai;
                retDuiArr.push(dui.pai_arr);
            }
        }

        //替换王牌
        var laiziIndex = 0;
        for (var i = retDuiArr.length -1; i > -1; i--) {
            for (var j = 0; j < retDuiArr[i].length; j++) {
                laiziIndex = laizi.indexOf(retDuiArr[i][j]);
                if (laiziIndex != -1) {
                    laizi.splice(laiziIndex, 1);
                    retDuiArr[i][j] = 21;
                }
            }
        }

        return retDuiArr;
    },
    /**
     * 清除小结桌面的显示信息
     */
    closeXiaojieZhuomian : function () {
        var self = this;
        //清理荒庄显示信息
        var panelResult = self.$('PanelResult');
        var containner = self.$('PanelResult.bg.containner');
        //隐藏按钮
        self.$("continue", panelResult).setVisible(true);
        self.$("btn_jiesuan", panelResult).setVisible(true);
        self.$("btn_zhuomian", panelResult).setVisible(true);
        self.$("bg.huangzhuang", panelResult).setVisible(false);
        self.$("bg", panelResult).setBackGroundColorOpacity(122);
        for (var i = 0; i < 4; i++) {
            self.$("pos" + i + ".cardPos", containner).removeAllChildren();
            self.$("pos" + i + ".score", containner).setVisible(false);
        }
        self.$("dipai", containner).setVisible(false);
        self.$("dipai.pos", containner).removeAllChildren();
    },
    handleHuangzhuang : function () {
        var self = this;
        var xiaojieData = self.zipaiManager.gameOverData;
        var panelResult = self.$('PanelResult');
        var containner = self.$('PanelResult.bg.containner');
        self.$('dipai', containner).setVisible(false);
        //隐藏按钮
        self.$("continue", panelResult).setVisible(false);
        self.$("btn_jiesuan", panelResult).setVisible(false);
        self.$("btn_zhuomian", panelResult).setVisible(false);

        //背景透明
        //self.$("bg", panelResult).setOpacity(255);
        self.$("bg", panelResult).setBackGroundColorOpacity(0);

        //显示荒庄
        self.$("bg.huangzhuang", panelResult).setVisible(true);

        //显示玩家信息
        var players = xiaojieData.players;
        var playerData = {};
        var wangNum = 0;
        var row = 0;
        var card;
        var pos;
        var cardPos;
        var scoreNode;
        for (var i = 0; i < players.length; i ++){
            playerData = players[i];
            wangNum = 0;
            for (var j = 0; j < playerData.shoupai.length; j++){
                if (playerData.shoupai[j] == 21) {
                    wangNum ++;
                }
            }
            row = self.zipaiManager.g_uid_to_pos[playerData.uid];
            pos = self.$("pos" + row, containner);
            cardPos = self.$("cardPos", pos);
            scoreNode = self.$("score", pos);
            //显示王牌
            for (var j = 0; j < wangNum; j++) {
                card = new Card();
                cardPos.addChild(card);
                card.setData(21, null, 1);
                card.setAnchorPoint(cc.p(0, 0.5));
                card.setPosition(cc.p(j * 60, 0));
            }
            //显示分数
            scoreNode.setVisible(true);
            scoreNode.setString(playerData.score);
        }

        //关闭荒庄界面
        setTimeout(function () {

            var touchLayer = self.getTouchLayer();
            cc.eventManager.resumeTarget(touchLayer, true);
            self.closeXiaojieZhuomian();
            self.showRoomClean();
            self.onReady();
        }, 5000);
    },
    /**
     * 设置自己的手牌(4200发牌特殊处理)
     */
    setUpCardsByFapai: function (isReplay) {
        var that = this;
        that.isMoving = true;
        that.isBeginTouch = true;
        var cardsNode = this.getCardsNode(0);
        cardsNode.setVisible(true);
        cardsNode.removeAllChildren();
        cardsNode.setScale(1);
        this.hideOk();
        this.setUpHX(0);
        this.cardScale = mCard.getCardScale();
        this.cardSize = mCard.getCardSize();
        var cardList = this.zipaiManager.CardList;

        var tmpCardList2 = [];
        for (var i = 0; i < cardList.length; i++) {
            tmpCardList2[i] = [];
            var cards = cardList[i];
            var isKan = false;
            //坎 不能移动
            if ((cards.length == 3 || cards.length == 4) && cards[0] == cards[1] && cards[1] == cards[2] && cards[0] != 21) {
                isKan = this.zipaiManager.isDaiKan;
            }
            for (var j = cards.length - 1; j >= 0; j--) {
                var card = new Card();
                tmpCardList2[i][j] = card;
                cardsNode.addChild(card);
                card.setData(cards[j], this);
                card.setRowColumn(j, i);
                card.setKan(isKan);
                card.setLocalZOrder(3 - j);
                card.setScale(this.cardScale);
                card.setVisible(false);
                var posx = i * (this.cardSize.w * this.cardScale - 2) - this.cardSize.w / 2;
                var is4Same = cards.length == 4 && cards[0] == cards[1] && cards[0] == cards[2] &&
                    cards[0] == cards[3] && cards[0] != 21;
                //var posy = j * (this.cardSize.h * this.cardScale - (is4Same ? 40 : 25)) - 10;
                var posy = j * (this.cardSize.h * this.cardScale - (is4Same ? 45 : 35)) - 30;
                if(that.isPaiOffSetY){
                    posy -= mCard.getCardOffsetY();
                }
                card.moveToPos = [posx, posy];
                card.delayTimeVal = i + j;
                var tag = (i + 1) * 100 + j + 1;
                card.setTag(tag);
            }
        }
        that.setUpCardsHuXi();
        that.setLeftCard();
        that.$('FunctionBtn.btn_refresh').setVisible(cardList.length > 0 && this.isBaiPai);

        if (that.zipaiManager.isReplay && that.playersNum == 4) {
            cardsNode.setPositionX(320);
            cardsNode.setScale(0.8);
        }
        else {
            cardsNode.setPositionX(DesignSize.width * 0.5 - cardList.length * 0.5 * this.cardSize.w * this.cardScale + this.cardSize.w);
        }

        var tmpCardList = [];
        var tmpCardPos = cc.p(cardList.length * 0.5 * this.cardSize.w * this.cardScale - this.cardSize.w, 500);
        //扁平处理
        for (var i = 0; i < tmpCardList2.length; i++) {
            for (var j = 0; j < tmpCardList2[i].length; j++) {
                tmpCardList.push(tmpCardList2[i][j]);
            }
        }

        var tmpCardListLen = tmpCardList.length;
        var tmpLocation = 0;
        var interval;
        var intervalFunc = function () {
            if (!that || tmpLocation == tmpCardListLen)
                return clearInterval(interval);
            if (tmpCardList[tmpLocation]){
                tmpCardList[tmpLocation].setVisible(true);
                tmpCardList[tmpLocation].setPosition(tmpCardPos);
                tmpCardList[tmpLocation].runAction(
                    cc.sequence(
                        cc.moveTo(0.15, tmpCardList[tmpLocation].moveToPos[0], tmpCardList[tmpLocation].moveToPos[1])
                    )
                );

                tmpLocation ++;
            }
            else {
                return clearInterval(interval);
            }
        };
        interval = setInterval(intervalFunc, 70);

        //重新设置
        that.scheduleOnce(function () {
            //冻结
            that.setFreezeCard();
            that.isMoving = false;
            that.isBeginTouch = false;
        }, 1.5);

        //如果有听牌提示且需要重新显示听，则调显示听
        if (that._needShowTingTip) {
            that.setTishiList();
            that._needShowTingTip = false;
        }
        if (that.afterGameStart && isReplay)
            that.afterGameStart();
    },
    /**
     * 设置自己的手牌
     */
    setUpCards: function (inData) {
        var that = this;
        var isReplay = false;
        if (inData){
            that.isFapai = false;
            if (inData["isReplay"]){
                isReplay = inData["isReplay"];
            }
            if (inData["isFapai"]) {
                that.isFapai = inData["isFapai"];
            }
            if (inData["isBlock"]){
                that.isBlockFapai = inData['isBlock'];
            }
        }
        else {
            that.isBlockFapai = false;
        }
        if (that.isFapai == true && that.isBlockFapai == false && isReplay == false){
            that.isFapai = false;
            that.isBlockFapai = false;
            that.setUpCardsByFapai(isReplay);
        }
        else if (that.isFapai == true && that.isBlockFapai == true && isReplay == false){
            return;
        }
        else {
            that.isMoving = true;
            that.isBeginTouch = true;
            var cardsNode = this.getCardsNode(0);
            cardsNode.setVisible(true);
            cardsNode.removeAllChildren();
            cardsNode.setScale(1);
            this.hideOk();
            this.setUpHX(0);
            this.cardScale = mCard.getCardScale();
            this.cardSize = mCard.getCardSize();
            var cardList = this.zipaiManager.CardList;

            for (var i = 0; i < cardList.length; i++) {
                var cards = cardList[i];
                var isKan = false;
                //坎 不能移动
                if ((cards.length == 3 || cards.length == 4) && cards[0] == cards[1] && cards[1] == cards[2] && cards[0] != 21) {
                    isKan = this.zipaiManager.isDaiKan;
                }
                for (var j = cards.length - 1; j >= 0; j--) {
                    var card = new Card();
                    cardsNode.addChild(card);
                    card.setData(cards[j], this);
                    card.setRowColumn(j, i);
                    card.setKan(isKan);
                    card.setLocalZOrder(3 - j);
                    card.setScale(this.cardScale);
                    var posx = i * (this.cardSize.w * this.cardScale - 2) - this.cardSize.w / 2;
                    var is4Same = cards.length == 4 && cards[0] == cards[1] && cards[0] == cards[2] &&
                        cards[0] == cards[3] && cards[0] != 21;
                    //var posy = j * (this.cardSize.h * this.cardScale - (is4Same ? 40 : 25)) - 10;
                    var posy = j * (this.cardSize.h * this.cardScale - (is4Same ? 45 : 35)) - 30;
                    if (that.isPaiOffSetY) {
                        posy -= mCard.getCardOffsetY();
                    }
                    card.setPosition(posx, posy);
                    var tag = (i + 1) * 100 + j + 1;
                    card.setTag(tag);
                }
            }
            that.setUpCardsHuXi();
            that.setLeftCard();
            that.$('FunctionBtn.btn_refresh').setVisible(cardList.length > 0 && this.isBaiPai);
            //冻结
            this.setFreezeCard();
            if (that.zipaiManager.isReplay && that.playersNum == 4) {
                cardsNode.setPositionX(320);
                cardsNode.setScale(0.8);
            } else {
                cardsNode.setPositionX(DesignSize.width * 0.5 - cardList.length * 0.5 * this.cardSize.w * this.cardScale + this.cardSize.w);
            }
            //重新设置
            that.scheduleOnce(function () {
                that.isMoving = false;
                that.isBeginTouch = false;
            }, 0.5);

            //如果有听牌提示且需要重新显示听，则调显示听
            if (that._needShowTingTip) {
                that.setTishiList();
                that._needShowTingTip = false;
            }
            if (that.afterGameStart && isReplay)
                that.afterGameStart();
        }
    },
    /**
     * 操作吃、碰、过、胡 按钮
     */
    onActionBtn: function (tag, target) {
        var self = this;
        if (self.isBeginTouch && self.movecard && self.positionBak) {
            if (self.zorderBak){
                self.movecard.setLocalZOrder(self.zorderBak);
            }
            self.movecardTmp.setVisible(false);
            self.movecard.stopAllActions();
            self.movecard.setPosition(self.movecardTmp.getPosition());
            self.movecard.setOpacity(255);
            self.movecard.runAction(cc.sequence(
                cc.moveTo(0.1, self.positionBak),
                cc.callFunc(function (sender) {
                    self.movexy = null;
                    self.movetoxy = null;
                    self.movetoxybak = null;
                    self.movecard = null;
                    self.positionBak = null;
                    self.isMoving = false;
                    self.isBeginTouch = false;
                    sender = null;
                })
            ));
            return;
        }
        self.showNo = self.showNo || target.paiArr[0];
        var hubtn = self.$('bt1', this.nAction);
        if (tag == 2) {
            if (hubtn.isVisible() == true) {
                //showMessageBox('当前可以胡牌，请问是否选择吃?', function () {
                    self.showChiSelect();
                //});
            } else {
                self.showChiSelect();
            }
        } else if (tag == 3) {
            if (hubtn.isVisible() == true) {
                //showMessageBox('当前可以胡牌，请问是否选择碰?', function () {
                    target.setVisible(false);
                    self.userAct(self.opType.OP_PENG, self.showNo, target);
                //});
            } else {
                target.setVisible(false);
                self.userAct(self.opType.OP_PENG, self.showNo, target);
            }
        } else if (tag == 1) {
            self.userAct(self.opType.OP_HU, self.showNo, target);
        } else if (tag == 4) {
            //过得时候判断一下  有没有 胡的选项,有的话,提示
            if (hubtn.isVisible() == true) {
                //showMessageBox('当前可以胡牌，请问是否选择过?', function () {
                    target.setVisible(false);
                    self.userAct(self.opType.OP_PASS, self.showNo, target);
                //});
            } else {
                target.setVisible(false);
                self.userAct(self.opType.OP_PASS, self.showNo, target);
            }
        }
        //王钓
        else if (tag == 6){
            self.userAct(self.opType.OP_HU, self.showNo, target, null, self.opType.OP_WANGDIAO);
        }
        //王闯
        else if (tag == 7){
            self.userAct(self.opType.OP_HU, self.showNo, target, null, self.opType.OP_WANGCHUANG);
        }
        //王炸
        else if (tag == 8){
            self.userAct(self.opType.OP_HU, self.showNo, target, null, self.opType.OP_WANGZHA);
        }
        Filter.remove(target);
    },
    /**
     * 显示吃碰胡动画
     * @param pNode
     * @param aniName
     * @param isloop
     */
    showBtnActAnimation: function (pNode, aniName, isloop) {
        var self = this;
        if (!cc.sys.isObjectValid(pNode) || aniName == '') {
            return;
        }
        pNode.removeAllChildren();
        pNode.setOpacity(255);

        var spineAni = playSpine(zipai.res.sp_anniu_json, aniName, isloop, function () {
        });
        pNode.addChild(spineAni);
        pNode.setScale(1.2, 1.2);
    }
});